#!/bin/sh

main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html
cat <<'EOF'
<script>
$(function(){
  $('#post_control_domain').on('submit', function(e){
    e.preventDefault();
    var data = "app=dnsmasq&action=post_control_domain&"+$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'ajax-fluid');
  });
});
</script>
EOF
cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-2-5">
				<h3>域名黑白控制列表</h3>
				<form class="pure-form" id="post_control_domain">
				<div class="pure-u-1">
					<label>模式
						<select name="mode">
							<option value="disable" `[ "$control_domain_mode" = "disable" ] && echo selected`>不启用</option>
							<option value="deny" `[ "$control_domain_mode" = "deny" ] && echo selected`>黑名单</option>
							<option value="allow" `[ "$control_domain_mode" = "allow" ] && echo selected`>白名单</option>
						</select>
					</label>
				</div>
				<div class="pure-u-1">
					<textarea rows="15" cols="40" name="white_domain_list">`grep -vE "^$|^#" $DOCUMENT_ROOT/apps/dnsmasq/domain-control.list | cut -d '/' -f2`</textarea>
				</div>
				<button class="pure-button" type="submit">保存</button>
				</form>
			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-2-5">
				<h3>说明</h3>
			</div>
		</div>
EOF
footer_html
}
post_control_domain()
{
sed -i "/^control_domain_mode=/d" $DOCUMENT_ROOT/apps/dnsmasq/dnsmasq_extra.conf
echo "control_domain_mode=\"$FORM_mode\"" >> $DOCUMENT_ROOT/apps/dnsmasq/dnsmasq_extra.conf
echo "$FORM_white_domain_list" > $DOCUMENT_ROOT/apps/dnsmasq/domain-control.list
$DOCUMENT_ROOT/apps/dnsmasq/F910dnsmasq.fw restart
echo "dns黑白名单修改成功!!" | main.sbin output_json 0
}

get_control_domain_list()
{
. /usr/share/libubox/jshn.sh
json_init
json_add_string "mode" "$control_domain_mode"
json_add_array domain_controlist
echo ''
for domain in `grep -vE "^$|^#" $DOCUMENT_ROOT/apps/dnsmasq/domain-control.list | cut -d '/' -f2`
do
json_add_string "" ${domain}
done
json_dump
}
get_dhcp_leases()
{
printf "Content-Type: text/html; charset=utf-8\r\n"
printf "\r\n"
cat /tmp/dhcp.leases
}
lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
eval `cat $DOCUMENT_ROOT/apps/dnsmasq/dnsmasq_extra.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi

